//
//  HMHomeViewController.swift
//  Swift3.0_Micro-blog-Sina
//
//  Created by Apple on 16/10/22.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit

private let statusCellIdentifier = "statusCellIdentifier"

class HMHomeViewController: HMVisitorViewController {
    
    // viewModel对象
    let statusListViewModel = HMStatusListViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        // 判断用户是否登录
        if !isLogin {
            // 设置访客视图信息
            visitorView?.setupVisitorViewInfo(imgName: nil, tilte: nil)
            return
        }
        setupUI()
        getStatusData()
}
    
    // MARK: - 监听事件
    @objc private func leftClick(){
        print("左侧按钮点击")
    }
    
    @objc private func rightClick(){
        print("右侧按钮点击")
        // 创建
        let tempVc = HMTempViewController()
        // 隐藏 tabbar
        tempVc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(tempVc, animated: true)
    }

    // MARK: - 设置视图
    private func setupUI(){
        setupNav()
        setupTableViewInfo()
    }
     // 设置tableView的视图
    private func setupTableViewInfo(){
        // 设置数据源方法
        tableView.dataSource = self
        // 注册cell
        tableView.register(HMStatusCell.self, forCellReuseIdentifier: statusCellIdentifier)
        // 设置行高
        tableView.rowHeight = 300
    }
    
    // MARK: - 设置导航
    private func setupNav(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(setHighlightedImgName: "navigationbar_pop", target: self, action: #selector(leftClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(setHighlightedImgName: "navigationbar_friendsearch", target: self, action: #selector(rightClick))
    }
}

extension HMHomeViewController{

    func getStatusData(){
        
        statusListViewModel.getHomeData { (isSuccess) in
            if !isSuccess{
                print("请求数据失败")
                return
            }
            self.tableView.reloadData()
        }
    }
}

// MARK: - tableViewDataSource
extension HMHomeViewController:UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: statusCellIdentifier, for: indexPath) as! HMStatusCell
//        cell.backgroundColor = HMRandomColor()
//        cell.textLabel?.text = dataArray[indexPath.row].text
        cell.stausViewModel = statusListViewModel.dataArray[indexPath.row]
        return cell
    }
}


















